package ThreadPool2;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

import static javax.swing.text.html.HTML.Tag.OL;

public class TCPServer_Thread extends Thread{
    public static int clientCount = 0;
    private static ServerSocket ss = null;
    private  ExecutorService threadpool = null;
       public TCPServer_Thread(){
           try {
               ss =  new ServerSocket(9999);
               int n = Runtime.getRuntime().availableProcessors();
              // threadpool = Executors.newFixedThreadPool(2*n);
               int poolSize = 2 * n;
          //     poolSize = 2;
               threadpool = new ThreadPoolExecutor(poolSize , poolSize , 0 , TimeUnit.MILLISECONDS , new LinkedBlockingDeque<>(1) ,new NamedThreadFactory("线程") , new RejectedExecutionHandler() {
                   @Override
                   public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                       System.out.println("reject task :"  + r.toString());
                   }
               }){
                   @Override
                   protected void beforeExecute(Thread t, Runnable r) {
                       super.beforeExecute(t, r);
                       System.out.println(" 当前的任务数：" +  this.getTaskCount() + "已完成的任务数：" + this.getCompletedTaskCount() + "等待执行 ——" + r.toString());
                   }

                   @Override
                   protected void afterExecute(Runnable r, Throwable t) {
                       super.afterExecute(r, t);
                       System.out.println("执行完毕 ——" + r.toString());
                   }

                   @Override
                   protected void terminated() {
                       super.terminated();
                       System.out.println("线程池结束" );
                   }
               };

           }
           catch (IOException e) {
               System.out.println("绑定客户端失败：" + e.getMessage() );
           }
       }
    public static void main(String[] args) throws IOException {
       TCPServer_Thread tcpServer_thread = new TCPServer_Thread();
       tcpServer_thread.start();
    }

    @Override
    public void run() {
        try {
            while (true)
            {
            Socket s = ss.accept();
             clientCount++;
            socketThread socketThread = new socketThread(s , clientCount);
            //socketThread.start();//交给线程池
                threadpool.execute(socketThread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            }
        }
    }

/*
ExecutorService service = new ExecutorService( 5 , 5 , OL , TimeUnit.MILLISECONDS , new SynchronousQueue<>()
    , Executors.defaultThreadFactory() , new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("reject task :"  + r.toString());
        }
    }) ;


    new ThreadPoolExecutor.DiscardOldestPolicy()
 */